package com.example.blueconn.manager;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.example.blueconn.Constant;
import com.example.blueconn.data.FxData;
import com.example.blueconn.data.LightData;
import com.example.blueconn.data.TempData;
import com.example.blueconn.utils.DataUtils;
import com.example.blueconn.utils.SPLightUtils;
import com.example.blueconn.utils.SPUtils;
import com.example.blueconn.utils.StringUtils;
import com.example.blueconn.utils.ThreadPoolUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhudongao
 * Desc
 * Date 2020/7/18
 */

public class DataMgr {
    private static final String TAG = "DataMgr";

    //原始字符串数据
    private static final MutableLiveData<String> rawData = new MutableLiveData<>();

    //图表刷新通知
    private static final MutableLiveData<Boolean> lightRefresh = new MutableLiveData<>();
    private static final MutableLiveData<Boolean> tempRefresh = new MutableLiveData<>();
    private static final Map<Integer, Map<String, Float>> dealData = new HashMap<>();

    // fx 系数 数据接收
    private static final MutableLiveData<FxData> fxData = new MutableLiveData<>();
    private static final Map<Integer, FxData> lightFxMap = new HashMap<>();
    private static final Map<Integer, FxData> tempFxMap = new HashMap<>();
    private static final MutableLiveData<LightData> realLightData = new MutableLiveData<>();
    private static final MutableLiveData<TempData> realTempData = new MutableLiveData<>();

    private static DataMgr dataMgr = new DataMgr();

//    public static volatile StringBuilder sb = new StringBuilder();

    public static MutableLiveData<Boolean> refresh = new MutableLiveData<>();
    public static MutableLiveData<Integer> LTSwitch = new MutableLiveData<>();
    public static MutableLiveData<Boolean> XSOk = new MutableLiveData<>();

    private DataMgr() {
        init();

    }

    public void init() {
        ThreadPoolUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rawData.observeForever(new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        if (!StringUtils.isEmpty(s)) {
                            Log.i(TAG, "raw string data received. ： " + s);
//                            realData.setValue(DataUtils.getResult(s));
//                            String raw = "DD3131302E303534373934202D31312E373532FF";
                            DataUtils.getResult(s);
//                            sb.append("原始数据：").append(s).append("\n");
                            refresh.postValue(true);
                        } else {
                            Log.w(TAG, "apply: data is empty!!!");
                        }
                    }
                });
                fxData.observeForever(fxData -> {
                    if (fxData != null) {
                        Log.i(TAG, "接收的data数据：" + fxData.toString());
                        if ("T".equals(fxData.dataType)) {
                            Log.i(TAG, "温度斜率 截距数据转换成功 : " + fxData.toString());
                            tempFxMap.put(fxData.number, fxData);
//                            sb.append("温度系数：斜率： ").append(fxData.slopA).append("; 截距：").append(fxData.nodalA).append("\n");
                            if (fxData.number == 5) {
                                SPUtils.setMap(Constant.TEMP_PARAMS_KEY, tempFxMap);
                                FastBleManager.getInstance().writeData(DataUtils.xishuOk());
                                XSOk.postValue(true);
                            }
                        } else if ("L".equals(fxData.dataType)) {
                            Log.i(TAG, "照度斜率 截距数据转换成功 : " + fxData.toString());
                            lightFxMap.put(fxData.number, fxData);
//                            sb.append("照度系数：斜率1： ").append(fxData.slopA).append("; 截距1：").append(fxData.nodalA).append("\n");
//                            sb.append("照度系数：斜率2： ").append(fxData.slopB).append("; 截距2：").append(fxData.nodalB).append("\n");
                            if (fxData.number == 6) {
                                SPLightUtils.setMap(Constant.LIGHT_PARAMS_KEY, lightFxMap);
                            }
                        }
                    }
                });

                ChartMgr.getInstance().initChartObserver();
            }
        });
    }

    public static DataMgr getInstance() {
        return dataMgr;
    }

    public void postRawData(String data) {
        Log.i(TAG, "postRawData: " + data);
        rawData.postValue(data);
    }

    public void postFxData(FxData data) {
        fxData.postValue(data);
    }

    /**
     * 清理平均值、最大值、最小值等数据
     */
    public static void clearDealData() {
        Collection<Map<String, Float>> values = dealData.values();
        for (Map<String, Float> value : values){
            value.put(Constant.TEMP_DATA_TOTAL, 0f);
            value.put(Constant.LIGHT_DATA_TOTAL, 0f);
            value.put(Constant.MAX_TEMP_DATA, 0f);
            value.put(Constant.MINI_TEMP_DATA, null);
            value.put(Constant.MINI_LIGHT_DATA, null);
            value.put(Constant.DATA_COUNT, 0f);
            value.put(Constant.AVERAGE_TEMP_DATA, 0f);
            value.put(Constant.AVERAGE_Light_DATA, 0f);
        }
    }

    /**
     * 清理打个设备deal数据
     */
    public static void clearDealDataByDevice(int deviceNum) {
        Map<String, Float> deviceMap = dealData.get(deviceNum);
        if (deviceMap != null) {
            deviceMap.put(Constant.TEMP_DATA_TOTAL, 0f);
            deviceMap.put(Constant.LIGHT_DATA_TOTAL, 0f);
            deviceMap.put(Constant.MAX_TEMP_DATA, 0f);
            deviceMap.put(Constant.MINI_TEMP_DATA, null);
            deviceMap.put(Constant.MINI_LIGHT_DATA, null);
            deviceMap.put(Constant.DATA_COUNT, 0f);
            deviceMap.put(Constant.AVERAGE_TEMP_DATA, 0f);
            deviceMap.put(Constant.AVERAGE_Light_DATA, 0f);
        }
    }

    public static MutableLiveData<Boolean> getLightRefresh() {
        return lightRefresh;
    }

    public static MutableLiveData<Boolean> getTempRefresh() {
        return tempRefresh;
    }

    public static Map<Integer, Map<String, Float>> getDealData() {
        return dealData;
    }


    public static Map<Integer, FxData> getLightFxMap() {
        return lightFxMap;
    }

    public static FxData getLightFxDataByNumber(int number) {
        FxData data = lightFxMap.get(number);
        if (data == null) {
            data = new FxData();
        }
        return data;
    }

    public static FxData getTempFxDataByNumber(int number) {
        FxData data = tempFxMap.get(number);
        if (data == null) {
            data = new FxData();
        }
        return data;
    }

    public static Map<Integer, FxData> getTempFxMap() {
        return tempFxMap;
    }

    public static MutableLiveData<LightData> getRealLightData() {
        return realLightData;
    }

    public static MutableLiveData<TempData> getRealTempData() {
        return realTempData;
    }

}
